package com.naizhuai.ui.mianbusiness.action;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.CardView;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.google.gson.Gson;
import com.naizhuai.R;
import com.naizhuai.network.exception.ApiException;
import com.naizhuai.ui.BaseActivity;
import com.naizhuai.ui.adapter.business.ActionAddImageAdapterHolder;
import com.naizhuai.ui.entity.ImageBean;
import com.naizhuai.ui.entity.LabelsConvertBean;
import com.naizhuai.ui.entity.SimpleKeyValueBean;
import com.naizhuai.ui.entity.mainbusiness.ActionBaseBean;
import com.naizhuai.ui.entity.mainbusiness.ActionCompareDataBean;
import com.naizhuai.ui.entity.mainbusiness.ActionEditContractActivityBean;
import com.naizhuai.ui.entity.mainbusiness.ActionSelectedProcessImageDetails;
import com.naizhuai.ui.entity.mainbusiness.ActionSubmitProcessTxtBean;
import com.naizhuai.ui.mianbusiness.action.presenter.ActionManageApiStoreIml;
import com.naizhuai.ui.mianbusiness.action.presenter.UploadImageApiStoreIml;
import com.naizhuai.ui.task.ActionSetting1Activity;
import com.naizhuai.util.BitmapUtils;
import com.naizhuai.util.ConstantsState;
import com.naizhuai.util.DialogUtils;
import com.naizhuai.util.ErrorViewCreateUtils;
import com.naizhuai.util.FileNameUtils;
import com.naizhuai.util.IntentUtils;
import com.naizhuai.util.MathUtils;
import com.naizhuai.util.NaizhuAILog;
import com.naizhuai.util.RxBus;
import com.naizhuai.util.UidUtil;
import com.yanzhenjie.album.Action;
import com.yanzhenjie.album.Album;
import com.yanzhenjie.album.AlbumFile;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import rx.subjects.PublishSubject;

/**
 * 创建行为页面2过程数据的展示
 */
public class ActionDetailTxtCreateActivity extends BaseActivity implements View.OnClickListener, ActionAddImageAdapterHolder.OnItemClickListener {
    private static final String TAG = "ActionDetailTxtCreateActivity";
    public static final String EDIT_OTHER_TXT = "other";

    //底部栏以及title的点击
    @BindView(R.id.back_icon)
    ImageView titleBack;

    @BindView(R.id.pre_step)
    TextView preStep;
    @BindView(R.id.edit)
    TextView preEdit;
    @BindView(R.id.next_step)
    TextView nextStep;
    //分析
    @BindView(R.id.edit_analysis_1)
    EditText analysis1;
    @BindView(R.id.edit_analysis_2)
    EditText analysis2;
    @BindView(R.id.edit_analysis_3)
    EditText analysis3;
    @BindView(R.id.edit_analysis_4)
    EditText analysis4;
    @BindView(R.id.edit_analysis_5)
    EditText analysis5;

    private EditText[] dataSourceSteps;

    //改前数据修改
    @BindView(R.id.compare_data_card)
    CardView compareDataCard;

    //公司设备选择下弹窗
    @BindView(R.id.equip_chosen_layout)
    LinearLayout equipLayout;

    @BindView(R.id.chosen_equip_item)
    TextView equipTxt;

    //选择数据结构的下弹窗
    private OptionsPickerView<LabelsConvertBean> pvDataOptions;
    //选择设备结构的选择下弹窗
    private OptionsPickerView<ActionEditContractActivityBean.EquipmentChosen> pvEquipmentOptions;
    @BindView(R.id.chosen_data_item)
    TextView dataViewDetail;
    @BindView(R.id.data_value)
    EditText dataValue;
    @BindView(R.id.count_type_details)
    EditText countType;
    @BindView(R.id.count_layout)
    LinearLayout countLayout;

    private LabelsConvertBean chosenConvertBean;

    private ActionEditContractActivityBean.EquipmentChosen chosenEquipment;

    private List<LabelsConvertBean> convertBeanList;

    private ActionEditContractActivityBean actionBean;

    private List<ActionEditContractActivityBean.EquipmentChosen> equipmentList;

    //细节图片0
    @BindView(R.id.create_action_images_card)
    CardView imageCard;
    //详细图片
    @BindView(R.id.image_data_list)
    RecyclerView imageAddList;
    //详细文字细节
//    @BindView(R.id.line_text_data_list)
//    RecyclerView imageDetailsList;
    @BindView(R.id.edit_process_1)
    EditText processEdit1;
    @BindView(R.id.edit_process_2)
    EditText processEdit2;
    @BindView(R.id.edit_process_3)
    EditText processEdit3;
    private EditText[] processEdit;
    @BindView(R.id.error_content)
    RelativeLayout errorLayout;
    @BindView(R.id.context_layout)
    NestedScrollView scrollView;

    private Subscription loadupSub;
    private Subscription submitSub;


    private ActionAddImageAdapterHolder imageContentAdapter;
//    private ActionCheckedEditAdapterHolder dataAdapter;
//    private ActionImageDetailsEditAdapterHolder detailsAdapter;

    private List<SimpleKeyValueBean> filePathStrs;

    private int checkType = IntentUtils.PREVIEW_FROM_EDIT;
    private String questionId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBaseData();
        initBasicView();
        imageAddList.post(new Runnable() {
            @Override
            public void run() {
                initImageRecyclerView();
                initListener();
                initView();
            }
        });
    }

    private void initView() {
        boolean enable = checkType == IntentUtils.PREVIEW_FROM_EDIT;
        requestScanData();
        initCheckTypeViews(enable);
    }

    private void requestScanData() {
        NaizhuAILog.e("questionId=" + questionId);
        ActionManageApiStoreIml.getInstance().getProcessDetails(questionId).subscribe(new Observer<ActionEditContractActivityBean>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                ApiException.showException(e, getActivityContext());
                scrollView.setVisibility(View.INVISIBLE);
                errorLayout.setVisibility(View.VISIBLE);
                errorLayout.removeAllViews();
                errorLayout.addView(ErrorViewCreateUtils.createNoNetworkView(getActivityContext(), v -> {
                    initErrorLayout();
                    requestScanData();
                }));
            }

            @Override
            public void onNext(ActionEditContractActivityBean actionEditContractActivityBean) {
                NaizhuAILog.e("onNext");
                scrollView.setVisibility(View.VISIBLE);
                errorLayout.setVisibility(View.GONE);
                actionBean = actionEditContractActivityBean;
                refreshData(actionEditContractActivityBean);
            }
        });
    }

    @Override
    protected void onNewIntent(Intent intent) {
        shortToast("返回了");
        checkType = IntentUtils.PREVIEW_FROM_SCAN;
        initView();
    }

    private void refreshData(ActionEditContractActivityBean actionBean) {
        convertBeanList.clear();
        convertBeanList.addAll(actionBean.getCompareDataList());
        equipmentList = actionBean.getEquipmentsList();
        int size = Math.min(dataSourceSteps.length, actionBean.getThinkingDetail().size());
        if (size > 0) {
            for (int index = 0; index < size; index++)
                dataSourceSteps[index].setText(actionBean.getThinkingDetail().get(index));
        }

        if (actionBean.getSelectedCompareData() != null && actionBean.getSelectedCompareData().size() > 0 && !TextUtils.isEmpty(actionBean.getSelectedCompareData().get(0).getPointId())) {
            ActionCompareDataBean compareData = actionBean.getSelectedCompareData().get(0);
            if (!TextUtils.isEmpty(compareData.getPointId())) {
                dataViewDetail.setText(String.format("%s.%s", compareData.getPointName(), compareData.getEnteringName()));
                dataValue.setText(compareData.getbValue());
                if (TextUtils.isEmpty(compareData.getbFormula()))
                    countLayout.setVisibility(View.GONE);
                else {
                    countLayout.setVisibility(View.VISIBLE);
                    countType.setText(compareData.getbFormula());
                }
            }
            if (!TextUtils.isEmpty(compareData.getMachineName())) {
                equipLayout.setVisibility(View.VISIBLE);
                dataValue.setGravity(Gravity.END);
                equipTxt.setText(compareData.getMachineName());
            } else {
                dataValue.setGravity(Gravity.CENTER);
                equipLayout.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(compareData.getPointId())) {
                chosenConvertBean = new LabelsConvertBean();
                chosenConvertBean.setKey(compareData.getPointId());
            }
        } else {
            if (checkType == IntentUtils.PREVIEW_FROM_EDIT) {
                compareDataCard.setVisibility(View.VISIBLE);
            } else {
                compareDataCard.setVisibility(View.GONE);
            }
        }

        filePathStrs.clear();
        filePathStrs.addAll(SimpleKeyValueBean.getTypeListValues(ActionAddImageAdapterHolder.URL_NET_IMAGE, actionBean.getProcessImages()));
        NaizhuAILog.e("filePathStrs.size()=" + filePathStrs.size());
        imageContentAdapter.notifyDataSetChanged();
        if (actionBean.getProcessDetail() != null && actionBean.getProcessDetail().size() > 0) {
            List<String> processList = actionBean.getProcessDetail();
            List<String> intList = new ArrayList<>();
            if (intList.size() > processList.size()) {
                for (int index = 0; index < processList.size(); index++)
                    intList.set(index, processList.get(index));
            } else {
                intList.clear();
                intList.addAll(processList);
            }
            int totalSize = Math.min(processEdit.length, intList.size());
            for (int index = 0; index < totalSize; index++)
                processEdit[index].setText(intList.get(index));
        }
    }

    private void initCheckTypeViews(boolean enable) {
        for (EditText edit : dataSourceSteps) {
            edit.setEnabled(enable);
        }
        for (EditText edit : processEdit) {
            edit.setEnabled(enable);
        }
        if (enable) {
            dataViewDetail.setOnClickListener(this);
            equipTxt.setOnClickListener(this);
        } else {
            dataViewDetail.setOnClickListener(null);
            equipTxt.setOnClickListener(null);
        }
        imageContentAdapter.setScanMode(!enable);
        imageContentAdapter.notifyDataSetChanged();
        preEdit.setEnabled(!enable);
        dataValue.setEnabled(enable);
        countType.setEnabled(enable);

    }

    private void initErrorLayout() {
        scrollView.setVisibility(View.INVISIBLE);
        errorLayout.setVisibility(View.VISIBLE);
        errorLayout.removeAllViews();
        errorLayout.addView(ErrorViewCreateUtils.createLoadingView(this));
    }

    private void initBasicView() {
        getTextUnderLine(dataViewDetail, getString(R.string.virtual_data_source));
        getTextUnderLine(equipTxt, getString(R.string.equipment_source));
        initErrorLayout();
        dataSourceSteps = new EditText[]{analysis1, analysis2, analysis3, analysis4, analysis5};
        processEdit = new EditText[]{processEdit1, processEdit2, processEdit3};
        RxBus.getDefault().toObservableSticky(Integer.class)
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer index) {
                        int deleteIndex = index;
                        if (filePathStrs.size() > 0) {
                            filePathStrs.remove(deleteIndex);
                            imageContentAdapter.notifyDataSetChanged();
                        }
                    }
                });
    }

    private void initBaseData() {
        filePathStrs = new ArrayList<>();
        convertBeanList = new ArrayList<>();

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            checkType = Integer.parseInt(bundle.getString(IntentUtils.ENTER_ACTIVITY_TYPE_KEY, String.valueOf(IntentUtils.PREVIEW_FROM_EDIT)));
            questionId = bundle.getString(ActionBaseBean.ACTION_ID_KEY);
        }
    }

    private void initListener() {
        titleBack.setOnClickListener(this);
        preEdit.setOnClickListener(this);
        preStep.setOnClickListener(this);
        nextStep.setOnClickListener(this);
        dataViewDetail.setOnClickListener(this);
        equipTxt.setOnClickListener(this);
        imageContentAdapter.setOnItemClickListener(this);
    }


    private void initImageRecyclerView() {
        //图片选择的横向的recyclerView
        imageContentAdapter = new ActionAddImageAdapterHolder(this, filePathStrs, imageAddList.getWidth());
        imageAddList.setHasFixedSize(true);
        imageAddList.setLayoutManager(new LinearLayoutManager(this, LinearLayout.HORIZONTAL, false) {
            @Override
            public boolean canScrollHorizontally() {
                return false;
            }
        });
        imageAddList.setItemAnimator(new DefaultItemAnimator());
        imageAddList.setAdapter(imageContentAdapter);
    }

    @Override
    public void onBackPressedWithoutDialog() {
        DialogUtils.showSureDialog(this, getString(R.string.lost_data), new View.OnClickListener() {
            @Override
            public void onClick(View dialog) {
                finish();
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_business_action_create_new_layout;
    }


    private void submitCreateTargetMessage() {
        for (int index = 0; index < filePathStrs.size(); index++) {
            NaizhuAILog.e("result.get(index).getPath()=" + filePathStrs.get(index).getValue());
        }
        if (filePathStrs.size() == 0) {
            submitAllMessage(null);
            return;
        }
        loadupSub = Observable.just(filePathStrs).map(new Func1<List<SimpleKeyValueBean>, List<String>>() {
            @Override
            public List<String> call(List<SimpleKeyValueBean> simpleKeys) {
                List<String> filePath = new ArrayList<>();
                for (int i = 0; i < simpleKeys.size(); i++) {
                    if (simpleKeys.get(i).getKey() == ActionAddImageAdapterHolder.LOCAL_FILE_IMAGE) {
                        String filename = simpleKeys.get(i).getValue();
                        Bitmap bitmap = BitmapFactory.decodeFile(filename);
                        File file = BitmapUtils.changeBitmap2File(bitmap,
                                ActionDetailTxtCreateActivity.this, i + "compare_temp." + FileNameUtils.getFileType(filename));
                        filePath.add(file.getPath());
                    }
                }
                return filePath;
            }
        })
                .subscribeOn(Schedulers.io())
                .flatMap(new Func1<List<String>, Observable<List<ImageBean>>>() {
                    @Override
                    public Observable<List<ImageBean>> call(List<String> strings) {
                        for (String sre : strings) {
                            NaizhuAILog.e("filrUp location=" + sre);
                        }
                        return UploadImageApiStoreIml.getInstance().uploadImagesByUrl(strings);
                    }
                })
                .subscribe(new Observer<List<ImageBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        hideWaitDialog();
                        ApiException.showException(e, getActivityContext(), true);
                    }

                    @Override
                    public void onNext(List<ImageBean> imageBeans) {
                        NaizhuAILog.e("上传完成");
                        hideWaitDialog();
                        List<String> imageUrls = new ArrayList<>();
                        for (ImageBean bean : imageBeans) {
                            imageUrls.add(bean.getUrl());
                        }
                        for (SimpleKeyValueBean bean : filePathStrs) {
                            if (bean.getKey() == ActionAddImageAdapterHolder.URL_NET_IMAGE)
                                imageUrls.add(bean.getValue());
                        }
                        submitAllMessage(imageUrls);
                    }
                });
        showWaitDiaLog(loadupSub);

    }

    private void submitAllMessage(List<String> processBean) {
        Map<String, String> subActionProcess = new HashMap<>();
        subActionProcess.put(ActionSubmitProcessTxtBean.QUESTION_ID_KEY, questionId);
        NaizhuAILog.e("QUESTION_ID_KEY=" + questionId);
        //分析详情的数据：dataSourceSteps
        List<String> sourceSteps = new ArrayList<>();
        for (EditText dataSourceStep : dataSourceSteps) {
            String str = dataSourceStep.getText().toString();
            if (!TextUtils.isEmpty(str))
                sourceSteps.add(str);
        }
        subActionProcess.put(ActionSubmitProcessTxtBean.THINKING_DETAILS_KEY, new UidUtil(false, sourceSteps).toString());
        //过程详情
        List<String> process = new ArrayList<>();
        for (EditText aProcessEdit : processEdit) {
            String str = aProcessEdit.getText().toString();
            if (!TextUtils.isEmpty(str))
                process.add(str);
        }
        NaizhuAILog.e("new UidUtil(false, process).toString()=" + new Gson().toJson(process));
        subActionProcess.put(ActionSubmitProcessTxtBean.PROCESS_DETAILS_KEY, new Gson().toJson(process));
        //提交的图片的urls结构
        if (processBean != null && processBean.size() > 0)
            subActionProcess.put(ActionSubmitProcessTxtBean.PROCESS_IMAGES_KEY, new Gson().toJson(processBean));
        NaizhuAILog.e("PROCESS_IMAGES_KEY.toString()=" + new Gson().toJson(processBean));
        //分析数据结构体
        ActionCompareDataBean change = new ActionCompareDataBean();
        if (chosenConvertBean != null && MathUtils.parseStringToDouble(dataValue.getText().toString()) != -1) {
            change.setPointId(chosenConvertBean.getKey());
            if (equipLayout.getVisibility() == View.VISIBLE) {
                if (chosenEquipment == null) {
                    DialogUtils.showPromptDialog(this, getString(R.string.equip_error));
                    return;
                } else {
                    change.setMachineKey(chosenEquipment.getEquipKey());
                    change.setMachineName(chosenEquipment.getEquipValue());
                }
            } else {
                change.setMachineKey("");
                change.setMachineName("");
            }
            change.setbValue(dataValue.getText().toString());
            change.setbFormula(countType.getText().toString());
            NaizhuAILog.e("ActionCompareDataBean change--json=" + new Gson().toJson(change));
            subActionProcess.put(ActionSubmitProcessTxtBean.CHANGE_DATA_KEY, new Gson().toJson(change));
            submitProcessMessage(subActionProcess);
        } else {
            if (!TextUtils.isEmpty(dataValue.getText().toString()) && MathUtils.parseStringToDouble(dataValue.getText().toString()) == -1) {
                DialogUtils.showPromptDialog(getActivityContext(), getString(R.string.number_data_error));
                return;
            }
            DialogUtils.showSureDialog(this, getText(R.string.before_data_message_is_not_finished).toString(), new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    submitProcessMessage(subActionProcess);
                }
            });
        }
    }

    private void submitProcessMessage(Map<String, String> subActionProcess) {
        submitSub = ActionManageApiStoreIml.getInstance().submitActionProcess(subActionProcess).subscribe(new Observer<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                hideWaitDialog();
                ApiException.showException(e, getActivityContext(), true);
                NaizhuAILog.e("submitActionProcess e=" + e.getMessage());
            }

            @Override
            public void onNext(String s) {
                hideWaitDialog();
                if (!ConstantsState.isResultOk(s)) {
                    shortToast(getResources().getText(R.string.action_fail).toString());
                } else {
                    shortToast(getResources().getText(R.string.action_ok).toString());
                    IntentUtils.startActivityWithString(new String[]{ActionBaseBean.ACTION_ID_KEY, IntentUtils.ENTER_ACTIVITY_TYPE_KEY},
                            new String[]{questionId, String.valueOf(IntentUtils.PREVIEW_FROM_EDIT)},
                            getActivityContext(), ActionEditContrastActivity.class);
                }
            }
        });
        showWaitDiaLog(submitSub);
    }

    private void preSave() {
        if (compareDataCard.getVisibility() == View.GONE)
            compareDataCard.setVisibility(View.VISIBLE);
        if (checkType == IntentUtils.PREVIEW_FROM_SCAN) {
            checkType = IntentUtils.PREVIEW_FROM_EDIT;
            initCheckTypeViews(true);
        }
    }


    private void nextStage() {
        if (checkType == IntentUtils.PREVIEW_FROM_EDIT) {
            submitCreateTargetMessage();
        } else {
            IntentUtils.startActivityWithString(new String[]{ActionBaseBean.ACTION_ID_KEY, IntentUtils.ENTER_ACTIVITY_TYPE_KEY},
                    new String[]{questionId, String.valueOf(IntentUtils.PREVIEW_FROM_EDIT)},
                    ActionDetailTxtCreateActivity.this, ActionEditContrastActivity.class);
        }
    }

    private PublishSubject<LabelsConvertBean> initOptionsLabel(List<LabelsConvertBean> result) {
        PublishSubject<LabelsConvertBean> subject = PublishSubject.create();
        List<LabelsConvertBean> options1Items = new ArrayList<>();
        List<List<LabelsConvertBean>> options2Items = new ArrayList<>();
        for (LabelsConvertBean label : result) {
            options1Items.add(label);
            ArrayList<LabelsConvertBean> temp = new ArrayList<>();
            if (label.getSubLabel().size() > 0) {
                temp.addAll(label.getSubLabel());
            }
            options2Items.add(temp);
        }
        pvDataOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                chosenConvertBean = options2Items.get(options1).get(options2);
                chosenEquipment=null;
                subject.onNext(chosenConvertBean);
            }
        })
                .setTitleText(getString(R.string.number_data_setting))
                .setCancelColor(getResources().getColor(R.color.title_text_color))
                .setSubmitColor(getResources().getColor(R.color.lightorange))
                .setTextColorCenter(getResources().getColor(R.color.title_text_color))
                .build();
        pvDataOptions.setPicker(options1Items, options2Items);
        pvDataOptions.show();
        return subject;
    }

    //初始化设备选择列表
    private PublishSubject<ActionEditContractActivityBean.EquipmentChosen> initEquipLabel(List<ActionEditContractActivityBean.EquipmentChosen> result) {
        PublishSubject<ActionEditContractActivityBean.EquipmentChosen> subject = PublishSubject.create();
        pvEquipmentOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                chosenEquipment = result.get(options1);
                subject.onNext(chosenEquipment);
            }
        })
                .setTitleText(getString(R.string.number_data_setting))
                .setCancelColor(getResources().getColor(R.color.title_text_color))
                .setSubmitColor(getResources().getColor(R.color.lightorange))
                .setTextColorCenter(getResources().getColor(R.color.title_text_color))
                .build();
        pvEquipmentOptions.setPicker(result);
        pvEquipmentOptions.show();
        return subject;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_icon:
                //title的返回键
                onBackPressed();
                break;
            //checkType = Integer.parseInt(bundle.getString(IntentUtils.ENTER_ACTIVITY_TYPE_KEY
            case R.id.pre_step:
                //上一步
                if (checkType == IntentUtils.PREVIEW_FROM_EDIT)
                    DialogUtils.showSureDialog(this, getString(R.string.lost_data), new View.OnClickListener() {
                        @Override
                        public void onClick(View dialog) {
                            IntentUtils.startActivityWithStringAndInt(new String[]{ActionBaseBean.ACTION_ID_KEY, ActionBaseBean.SCHEME_ID_KEY, IntentUtils.ENTER_ACTIVITY_TYPE_KEY},
                                    new String[]{questionId, actionBean.getSchemeId(), String.valueOf(checkType)},
                                    new String[]{ActionSetting1Activity.PAGESTATE}, new int[]{ActionSetting1Activity.ONLYDISPLAY},
                                    ActionDetailTxtCreateActivity.this, ActionSetting1Activity.class);
                        }
                    });
                else {
                    IntentUtils.startActivityWithStringAndInt(new String[]{ActionBaseBean.ACTION_ID_KEY, ActionBaseBean.SCHEME_ID_KEY, IntentUtils.ENTER_ACTIVITY_TYPE_KEY},
                            new String[]{questionId, actionBean.getSchemeId(), String.valueOf(checkType)},
                            new String[]{ActionSetting1Activity.PAGESTATE}, new int[]{ActionSetting1Activity.ONLYDISPLAY},
                            this, ActionSetting1Activity.class);
                }
                finish();
                break;
            case R.id.edit:
                //预存/编辑
                preSave();
                break;
            case R.id.next_step:
                //下一步
                nextStage();
                break;
            case R.id.chosen_data_item:
                initOptionsLabel(convertBeanList).subscribe(new Action1<LabelsConvertBean>() {
                    @Override
                    public void call(LabelsConvertBean labelsConvertBean) {
                        equipTxt.setText(getString(R.string.equipment_source));
                        if (Arrays.asList(LabelsConvertBean.EQUIP_STATIC_CHOSEN_KEY).contains(labelsConvertBean.getKey())) {
                            equipLayout.setVisibility(View.VISIBLE);
                            dataValue.setGravity(Gravity.END);
                        } else {
                            equipLayout.setVisibility(View.GONE);
                            dataValue.setGravity(Gravity.CENTER);
                        }
                        dataViewDetail.setText(String.format("%s.%s", labelsConvertBean.getLabel(), labelsConvertBean.getEnteringName()));
                    }
                });
                break;
            case R.id.chosen_equip_item:
                initEquipLabel(equipmentList).subscribe(new Action1<ActionEditContractActivityBean.EquipmentChosen>() {
                    @Override
                    public void call(ActionEditContractActivityBean.EquipmentChosen equipmentChosen) {
                        equipTxt.setText(equipmentChosen.getEquipValue());
                    }
                });
                break;
        }
    }


    @Override
    public void onItemClick(View view, int position, int indexType) {
        switch (indexType) {
            case ActionAddImageAdapterHolder.ITEM_IMAGE_CONTENT:
                ActionSelectedProcessImageDetails details = new ActionSelectedProcessImageDetails(filePathStrs, position, checkType == IntentUtils.PREVIEW_FROM_EDIT);
                IntentUtils.startActivityWithObject(ActionShowChoseImageActivity.IMAGE_DATA_DETAILS, details, this, ActionShowChoseImageActivity.class);
                break;
            case ActionAddImageAdapterHolder.ITEM_ADD_ICON:
                if (checkType == IntentUtils.PREVIEW_FROM_SCAN)
                    return;
                int count = 3 - filePathStrs.size();
                Album.image(this) // 选择图片。
//                        .singleChoice()
                        .multipleChoice()
                        .requestCode(200)
                        .camera(true)
                        .columnCount(3)
                        .selectCount(count)
                        .onResult(new Action<ArrayList<AlbumFile>>() {
                            @Override
                            public void onAction(int requestCode, @NonNull ArrayList<AlbumFile> result) {
                                //filePathStrs
                                for (AlbumFile file : result) {
                                    filePathStrs.add(new SimpleKeyValueBean(ActionAddImageAdapterHolder.LOCAL_FILE_IMAGE, file.getPath()));
                                    imageContentAdapter.notifyDataSetChanged();
                                }
                            }
                        }).start();
                break;
        }
    }

}